package com.nwpu.am.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.nwpu.am.mapper.RuleMapper;
import com.nwpu.am.pojo.dto.RuleDTO;
import com.nwpu.am.pojo.dto.page.RulePageDTO;
import com.nwpu.am.pojo.entity.Rule;
import com.nwpu.am.result.PageResult;
import com.nwpu.am.service.RuleService;
import com.nwpu.am.utils.id_generator.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * RuleServiceImpl
 *
 * @author Fbf
 * @version 1.0
 * {@code @date} 2024/6/6
 */
@Service
@Slf4j
public class RuleServiceImpl implements RuleService {
    @Resource
    private RuleMapper ruleMapper;
    @Resource
    IdGenerator idGenerator;


    @Override
    public ArrayList<Rule> getAllRule() {
        return ruleMapper.selectAllRule();
    }

    @Override
    public int addRule(RuleDTO ruleDTO) {
        long id = idGenerator.nextId();
        Rule rule = Rule.builder()
                .ruleId(id)
                .premiseKnowledgeId(ruleDTO.getPremiseKnowledgeId())
                .conclusionKnowledgeId(ruleDTO.getConclusionKnowledgeId())
                .probability(ruleDTO.getProbability())
                .build();
        return ruleMapper.addRule(rule);
    }

    @Override
    public int deleteRule(long ruleId){
        return ruleMapper.deleteRule(ruleId);
    }

    @Override
    public Rule selectById(long ruleId){
        return ruleMapper.selectById(ruleId);
    }

    @Override
    public int update(Rule rule) {
        return ruleMapper.update(rule);
    }

    @Override
    public PageResult<Rule> pageQuery(RulePageDTO rulePageDTO) {
        // 开启分页插件
        PageMethod.startPage(rulePageDTO.getPage(), rulePageDTO.getPageSize());
        // 调用分页方法
        Page<Rule> page = ruleMapper.pageQuery(rulePageDTO);

        long total = page.getTotal();
        List<Rule> result = page.getResult();

        return new PageResult<>(total, result);
    }
}
